﻿if (typeof Modules == "undefined") Modules = {};

Modules.Products = {
    CatID: '',
    ID: '',
    ViewTemplate: null,
    TemplatePath: '../../uploads/template/product/',

    Init: function () {
        var _this = this;

        _this.ID = $.GetURL('ID');
        _this.CatID = $.GetURL('CatID');
        _this.ViewTemplate = $.GetURL('VTNo') || '01';

        if (_this.ID != null && _this.ID != '') {
            _this.LoadDetail(_this.ID);
        }
        else if (_this.CatID != null && _this.CatID != '') {
            var rowInPage = $.GetURL('ipage') || 100,
                pageIndex = $.GetURL('page') || 1;
            _this.LoadList(_this.CatID, rowInPage, pageIndex);
        }
        else {
            _this.LoadList();
        }
    },
    Show: function (data) {
        $('#content-body').html(data);
    },
    LoadList: function (catID, rowInPage, pageIndex) {
        var _this = this,
            inputValue = '<RequestParams Type="21" Context="GetResource" {0}/>',
            ext = '';
        if (catID) {
            ext = 'ID="' + catID + '"';
        }
        inputValue = $.string.Format(inputValue, ext);

        Modules.ServiceData.GetContextData(inputValue, function (data) {
            if (data[1].length > 0) {
                if (typeof rowInPage == 'undefined' || rowInPage == '') {
                    rowInPage = 4;
                }
                if (typeof pageIndex == 'undefined' || pageIndex == '') {
                    pageIndex = 1;
                }
                var arrInputValue = [],
                    tempResource = '<RequestParams Type="22" ParentID="{0}" Context="GetResource" RowsPerPage="{1}" PageIndex="{2}"/>';
                for (var i = 0; i < data[1].length; i++) {
                    var it = data[1][i];
                    if (it && it.IDKey) {
                        arrInputValue.push($.string.Format(tempResource, it.IDKey, rowInPage, pageIndex));
                    }
                }
                if (arrInputValue.length) {
                    Modules.ServiceData.GetContextData(arrInputValue.join(''), function (detail) {
                        _this.ShowList(data[1], detail);
                    });
                }
            }
        });
    },
    ShowList: function (list, detail) {
        var path = this.TemplatePath + this.ViewTemplate;
        $('#content-body').load(path + '/list.htm', function () {
            $.getScript(path + '/list.js', function () {
                if (ProductList) {
                    ProductList.Load(list, detail);
                }
            });
        });
    },
    LoadDetail: function (ID) {
        var _this = this,
            tempResource = '<RequestParams Type="22" ID="{0}" Context="GetResource"/>'
                        + '<RequestParams Type="1" ResourceID="{0}" Context="GetData"/>'
                        + '<RequestParams Type="2" ResourceID="{0}" Context="GetData"/>'
                        + '<RequestParams Type="3" ResourceID="{0}" Context="GetData"/>'
                        + '<RequestParams Type="4" ResourceID="{0}" Context="GetData"/>';
        var inputValue = $.string.Format(tempResource, ID);

        Modules.ServiceData.GetContextData(inputValue, function (data) {
            if (data) {
                _this.ShowDetail(data);
            }
        });
    },
    ShowDetail: function (data) {
        var path = this.TemplatePath + this.ViewTemplate;
        $('#content-body').load(path + '/detail.htm', function () {
            $.getScript(path + '/detail.js', function () {
                if (ProductDetail) {
                    ProductDetail.Load(data);
                }
            });
        });
    }
};
$(function () {
    Modules.Products.Init();
});
function GetRootPath() {
    var rootPath = '',
        loc = window.location.host;
    if (loc == 'tuantrang' || loc == 'localhost') {
        rootPath = 'http://thongthuongdieselengine.com/';
    }
    return rootPath;
};
